package com.kedacom.demo.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

public class HttpClientUtil {

	public HttpClientUtil() {
	}

	public static String doGet(String url) throws ClientProtocolException, IOException {
		HttpGet httpGet = new HttpGet(url);
		HttpResponse httpResponse = null;
		StringBuffer responseStringBuffer = new StringBuffer();

		httpResponse = new DefaultHttpClient().execute(httpGet);

		if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
			HttpEntity het = httpResponse.getEntity();
			InputStream is = het.getContent();
			BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
			String readLine = null;
			while ((readLine = br.readLine()) != null) {
				responseStringBuffer.append(readLine);
			}
			is.close();
			br.close();
		}
		return responseStringBuffer.toString();
	}

	public static String doPost(String url, List<BasicNameValuePair> params) throws IOException {
		HttpPost httpPost = new HttpPost(url);
		HttpEntity httpEntity;
		HttpClient client = new DefaultHttpClient();
		HttpResponse response;
		StringBuffer responseStringBuffer = new StringBuffer();

		httpEntity = new UrlEncodedFormEntity(params, "UTF-8");
		httpPost.setEntity(httpEntity);

		response = client.execute(httpPost);
		int statuscode = response.getStatusLine().getStatusCode();
		if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) || (statuscode == HttpStatus.SC_MOVED_PERMANENTLY)
				|| (statuscode == HttpStatus.SC_SEE_OTHER) || (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
			// 读取新的 URL 地址
			Header header = response.getFirstHeader("location");
			if (header != null) {
				String newuri = header.getValue();
				if ((newuri == null) || (newuri.equals(""))) {
					newuri = "/";
				}
				HttpGet httpget = new HttpGet(newuri);
				client = new DefaultHttpClient();
				response = client.execute(httpget);
			}
		}
		// 判断连接是否成功
		if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
			HttpEntity het = response.getEntity();
			InputStream is = het.getContent();
			BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
			String readLine = null;
			while ((readLine = br.readLine()) != null) {
				responseStringBuffer.append(readLine);
			}
			is.close();
			br.close();
		} else {
			System.out.println("非200 k 状态值 = " + response.getStatusLine().getStatusCode());
		}
		return responseStringBuffer.toString();
	}
}
